{extend name="../admin/common/base" /}
{block name="body"}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">插件管理列表</div>
                        <div class="layui-card-body admin-table">
                            <div class="layui-row admin-table-action">
                                <div class="layui-col-md6 tool-action">
                                    <button id="layui-icon-refresh"
                                            class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary">
                                        <i class="layui-icon layui-icon-refresh"></i>
                                    </button>
                                </div>
                            </div>
                            <div id="list-table" lay-filter="list-table"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="my_js"}
<script type="text/html" id="menu">
    {{#  if(d.create_time){ }}
    <a class="layui-btn layui-btn-xs layui-bg-gray" data-title="{{d.name}}-配置"
       data-href="/admin/addons/options/{{d.d_id}}.html"
       admin-event="formLayer">配置</a>
    <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="uninstall">卸载</a>
    {{#  }else{ }}
    <a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="install">安装</a>
    {{#  } }}
</script>
<script type="text/html" id="status">
    {{#  if(d.create_time){ }}
    <input type="checkbox" name="status" value="{{d.d_id}}"
           lay-skin="switch"
           lay-text="ON|OFF" lay-filter="status" {{
           d.status== 1 ? 'checked' : '' }}>
    {{#  } }}
</script>
<script>
    layui.use(['element', 'admin', 'table', 'form'], function () {
        var table = layui.table
            , admin = layui.admin
            , form = layui.form
            , $ = layui.$;
        var list_table = admin.table(table, 'list-table', "{:url('admin/addons/index')}", {
            page: false
            , cols: [[
                {field: 'name', title: '插件名称', minWidth: 100}
                , {field: 'd_id', title: '标识', minWidth: 100}
                , {field: 'description', title: '描述', minWidth: 100}
                , {field: 'version', align: 'center', title: '版本号', width: 80}
                , {field: 'app_id', align: 'center', title: '插件id', minWidth: 100}
                , {field: 'author', align: 'center', title: '作者', width: 90}
                , {field: 'author_url', title: '插件官网', minWidth: 100}
                , {field: 'demo_url', title: '演示地址', minWidth: 100}
                , {field: 'status', title: '状态', templet: '#status', width: 90}
                , {fixed: 'right', align: 'center', title: '操作', templet: '#menu', width: 140}
            ]]
        });

        $('#layui-icon-refresh').click(function () {
            list_table.reload();
        });
        form.on('switch(status)', function (obj) {
            var d_id = this.value;
            admin.ajax("/api/admin/addons/toggle/" + d_id, '', function (data) {
                if (data.code == 1001) {
                    layer.msg(data.msg);
                } else {
                    layer.msg(data.msg, {icon: 2});
                }
            }, '', 'get');
        });
        admin.enter(function () {
            $('.search-submit').click();
        });
        $('.search-submit').click(function () {
            var keyword = $("input[name='keyword']").val();
            list_table.reload({
                where: {
                    keyword: keyword
                }
            });
        });
        table.on('tool(list-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            var tr = obj.tr;
            if (layEvent === 'uninstall') {
                layer.confirm('确定卸载' + data.name + "吗？", {icon: 3, title: '提示'}, function (index) {
                    admin.ajax("/api/admin/addons/uninstall/" + data.d_id, '', function (data) {
                        if (data.code == 1001) {
                            list_table.reload();
                            layer.close(index);
                            layer.msg(data.msg);
                        } else {
                            layer.msg(data.msg);
                        }
                    }, '', 'get');
                });
            } else if (layEvent === 'install') {
                layer.confirm('确定安装' + data.name + "吗？", {icon: 3, title: '提示'}, function (index) {
                    admin.ajax("/api/admin/addons/install/" + data.d_id, '', function (data) {
                        if (data.code == 1001) {
                            list_table.reload();
                            layer.close(index);
                            layer.msg(data.msg);
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }, '', 'get');
                });
            }
        });
    });
</script>
{/block}